package com.ruoyi.serviceItem.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.serviceItem.mapper.ServiceItemMapper;
import com.ruoyi.serviceItem.domain.ServiceItem;
import com.ruoyi.serviceItem.service.IServiceItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class ServiceItemServiceImpl implements IServiceItemService 
{
    @Autowired
    private ServiceItemMapper serviceItemMapper;


    @Transactional
    @Override
    public int toggleServiceStatus(Long serviceId, String status) throws ServiceException {
        // 检查是否存在未完成订单
        if (checkServiceItemInUse(serviceId)) {
            throw new ServiceException("存在关联的未完成订单，无法下架");
        }

        ServiceItem item = new ServiceItem();
        item.setServiceId(serviceId);
        item.setExt1(status);
        return serviceItemMapper.updateServiceItem(item);
    }

    @Override
    public boolean checkServiceItemInUse(Long serviceId) {
        int count = serviceItemMapper.checkServiceItemInUse(serviceId);
        return count > 0;
    }

    /**
     * 查询服务项目
     * 
     * @param serviceId 服务项目主键
     * @return 服务项目
     */
    @Override
    public ServiceItem selectServiceItemByServiceId(Long serviceId)
    {
        return serviceItemMapper.selectServiceItemByServiceId(serviceId);
    }

    /**
     * 查询服务项目列表
     * 
     * @param serviceItem 服务项目
     * @return 服务项目
     */
    @Override
    public List<ServiceItem> selectServiceItemList(ServiceItem serviceItem)
    {
        return serviceItemMapper.selectServiceItemList(serviceItem);
    }

    /**
     * 新增服务项目
     * 
     * @param serviceItem 服务项目
     * @return 结果
     */
    @Override
    public int insertServiceItem(ServiceItem serviceItem)
    {
        serviceItem.setCreateTime(DateUtils.getNowDate());
        return serviceItemMapper.insertServiceItem(serviceItem);
    }

    /**
     * 修改服务项目
     * 
     * @param serviceItem 服务项目
     * @return 结果
     */
    @Override
    public int updateServiceItem(ServiceItem serviceItem)
    {
        return serviceItemMapper.updateServiceItem(serviceItem);
    }

    /**
     * 批量删除服务项目
     * 
     * @param serviceIds 需要删除的服务项目主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemByServiceIds(Long[] serviceIds)
    {
        return serviceItemMapper.deleteServiceItemByServiceIds(serviceIds);
    }

    /**
     * 删除服务项目信息
     * 
     * @param serviceId 服务项目主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemByServiceId(Long serviceId)
    {
        return serviceItemMapper.deleteServiceItemByServiceId(serviceId);
    }



}
